package com.infolab.aims.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.FactoryUtils;
import org.apache.commons.collections.list.LazyList;
import org.hibernate.validator.constraints.NotBlank;

import com.infolab.aims.common.SpecialMuLineToStringStyle;
import com.infolab.aims.common.SpecialToStringStyle;
import com.infolab.aims.common.bean.AbstractModelBean;
import com.infolab.aims.common.bean.ContactInfo;
import com.infolab.aims.domain.asset.AssetModel;



import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;



@Entity
@Table(name="AMTB_VENDOR")
public class Vendor extends AbstractModelBean implements Serializable {
	
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private Integer vendorId;
	
	@NotBlank(message="{error.empty}")
	private String vendorName;
	
	@NotBlank(message="{error.empty}")
	private String vendorType;
	
	private ContactInfo contactInfo;
	
	@Column(columnDefinition="number(1,0) default 1 not null")
	private boolean active;
	
	@OneToMany(mappedBy="vendor",cascade=CascadeType.ALL)
	private List<VendorAsset> vendorAssets = LazyList.decorate(new ArrayList(),
		      FactoryUtils.instantiateFactory(VendorAsset.class));

	public Integer getVendorId() {
		return vendorId;
	}

	public void setVendorId(Integer vendorId) {
		this.vendorId = vendorId;
	}

	public String getVendorName() {
		return vendorName;
	}

	public void setVendorName(String vendorName) {
		this.vendorName = vendorName;
	}

	public String getVendorType() {
		return vendorType;
	}

	public void setVendorType(String vendorType) {
		this.vendorType = vendorType;
	}

	public ContactInfo getContactInfo() {
		return contactInfo;
	}

	public void setContactInfo(ContactInfo contactInfo) {
		this.contactInfo = contactInfo;
	}

	public boolean isActive() {
		return active;
	}

	public void setActive(boolean active) {
		this.active = active;
	}
	
	public List<VendorAsset> getVendorAssets() {
		return vendorAssets;
	}

	public void setVendorAssets(List<VendorAsset> vendorAssets) {
		this.vendorAssets = vendorAssets;
	}
	
	public VendorAsset findVendorAsset(AssetModel assetModel) {
		
		if(vendorAssets!=null)
		{
			for(int i=0;i<vendorAssets.size();i++)
			{
				if(vendorAssets.get(i).getAssetModel().getAssetModelId().intValue()==assetModel.getAssetModelId().intValue())
				{
					return vendorAssets.get(i); 
				}
			}
		}
		return null;
	}
	
	public String getAuditContentHead()
	{
		ContactInfo contInfo = contactInfo!=null?contactInfo:new ContactInfo();
		
		return new ToStringBuilder(this,SpecialToStringStyle.SPECIAL_STYLE)
			.append("Vendor Name",vendorName)
			.append("Vendor Type",vendorType)
			.append("Mobile No",contInfo.getMobileNo()!=null?contInfo.getMobileNo():"")
			.append("Contact Address",contInfo.getContactAddress()!=null?contInfo.getContactAddress():"")
			.append("Contact Postal  Code",contInfo.getContactPostalCode()!=null?contInfo.getContactPostalCode():"")
			.append("Email",contInfo.getEmail()!=null?contInfo.getEmail():"")
			.append("Fax No",contInfo.getFaxNo()!=null?contInfo.getFaxNo():"")
			.toString();
	}
	
	
	public String getAuditContentDetail()
	{
		return super.getAuditContentDetail();
	}
	
	

	public String getAuditContentHeadForUpdateAssetPrices()
	{
		
		return new ToStringBuilder(this,SpecialToStringStyle.SPECIAL_STYLE)
			.append("Vendor Name",vendorName)
			.append("Vendor Type",vendorType)			
			.toString();
	}
	
	public String getAuditContentDetailForUpdateAssetPrices()
	{
		ToStringBuilder builder = new ToStringBuilder(this,SpecialMuLineToStringStyle.SPECIAL_MULINE_STYLE);
		if(vendorAssets!=null && CollectionUtils.isNotEmpty(vendorAssets))
		{
			int i = 0;
			for(VendorAsset vendorAsset : vendorAssets)
			{
				if(!vendorAsset.isDeleted()){
					builder.append("Item " + (++i), vendorAsset.getAuditContentHead());
				}
			}
			String str = builder.toString();
			return StringUtils.substring(str, str.indexOf("[")+1, str.lastIndexOf("]")-1);
		}			
		return super.getAuditContentDetail();
	}
	
	
	
	public String getAuditCode()
	{
		if(StringUtils.isNotEmpty(vendorName)){
			return vendorName;
		}
		return super.getAuditCode();
	}

	public static final String VENDORTYPE_NEW="NEW";
	public static final String VENDORTYPE_REPAIR="REPAIR";
}
